package cn.dianhun.om.jpa.controller;


import cn.dianhun.om.jpa.pojo.StuEntity;
import cn.dianhun.om.jpa.pojo.TeacherEntity;
import cn.dianhun.om.jpa.repository.StuRepository;
import cn.dianhun.om.jpa.repository.TeacherRepository;
import cn.dianhun.om.jpa.utills.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author cyt
 * @create 2020-07-08 下午 10:30
 */

@Component
@Slf4j
@RestController
public class testOneToMany {

    @Autowired
    StuRepository stuRepository;
    @Autowired
    TeacherRepository teacherRepository;

    @ApiOperation("测试一对多")
    @PostMapping("/test")
    public R testOneToMany() {

        StuEntity stuEntity = new StuEntity();
        stuEntity.setStuName("小崔");
        stuEntity.setStuClass("一班");

        StuEntity stuEntity2 = new StuEntity();
        stuEntity2.setStuName("小崔");
        stuEntity2.setStuClass("二班");

        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setTeacherName("王老师");
        teacherEntity.setTeacherCourse("高数");

        // 设置关系
        // teacherEntity.getStuEntities().add(stuEntity);

        // 设置关系
        stuEntity.setTeacherEntity(teacherEntity);
        stuEntity2.setTeacherEntity(teacherEntity);

        teacherRepository.save(teacherEntity);
        stuRepository.save(stuEntity);
        stuRepository.save(stuEntity2);
        return R.ok();
    }

    @ApiOperation("放弃一里面的对外键的维护")
    @PostMapping("/test2")
    public R testOneToMany2() {

        StuEntity stuEntity = new StuEntity();
        stuEntity.setStuName("小崔");
        stuEntity.setStuClass("一班");


        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setTeacherName("王老师");
        teacherEntity.setTeacherCourse("高数");

        // 设置关系， 放弃一里面的对外键的维护
        stuEntity.setTeacherEntity(teacherEntity);
        // 设置关系
        teacherEntity.getStuEntities().add(stuEntity);
        teacherRepository.save(teacherEntity);
        stuRepository.save(stuEntity);
        return R.ok();
    }


    /***
     * 级联操作要设置要 操作的主体， 在操作主体上面配置级联。
     */
    /**
     * 级联添加
     *
     * @return
     */
    @ApiOperation("级联添加")
    @PostMapping("/test3")
    public R testOneToMany3() {

        StuEntity stuEntity = new StuEntity();
        stuEntity.setStuName("小崔");
        stuEntity.setStuClass("一班");


        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setTeacherName("王老师");
        teacherEntity.setTeacherCourse("高数");


        // 设置关系
        stuEntity.setTeacherEntity(teacherEntity);
        teacherEntity.getStuEntities().add(stuEntity);
        teacherRepository.save(teacherEntity);

        return R.ok();
    }

    /**
     * 级联删除
     *
     * @return
     */

    @ApiOperation("级联删除")
    @PostMapping("/test4")
    public R testOneToMany4() {
        // 1、查询数据
        Optional<TeacherEntity> byId = teacherRepository.findById(15);
        TeacherEntity teacherEntity = byId.get();
        //2 、删除数据
        teacherRepository.delete(teacherEntity);
        return R.ok();
    }
}
